//
//  YZGAlertView.m
//  Masonry
//
//  Created by LXY on 16/6/14.
//  Copyright © 2016年 AVGD. All rights reserved.
//

#import "MJUIAlertView.h"
#import "MJUIAlertContentView.h"
@interface MJUIAlertView()<UIGestureRecognizerDelegate>

@property (nonatomic, strong) MJUIAlertContentView *viewContent;
@property (nonatomic, strong) MJUIAlertViewConfigure *configure;


@end

@implementation MJUIAlertView
+ (void)alertViewWithTitle:(NSString *)title message:(NSString *)message configure:(MJUIAlertViewConfigure *)configure confirmBlock:(void(^)(void))confirmBlock cancelBlock:(void(^)(void))cancelBlock {
    
    MJUIAlertView *blackView;
    if(configure.contentView){
        blackView = [[MJUIAlertView alloc] initWithFrame:configure.contentView.frame];//黑色阴影
        [configure.contentView addSubview:blackView];
    }else{
        UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
        blackView = [[MJUIAlertView alloc] initWithFrame:keyWindow.frame];//黑色阴影
        [keyWindow addSubview:blackView];
    }
    
    MJUIAlertContentView *viewContent = blackView.viewContent;
    viewContent.labTitle.text = title;
    viewContent.labContent.text = message;

    blackView.confirmBlock = confirmBlock;
    blackView.cancelBlock = cancelBlock;
    if(configure){
        blackView.configure = configure;
    }

    
}

- (void)dismissWindow{
    if(self.configure.disableDismissWindow){
        
    }else{
        [self dismiss];
    }
}

- (MJUIAlertContentView *)viewContent {
    if (_viewContent == nil) {
        __weak typeof(self) weakSelf = self;
        _viewContent = [MJUIAlertContentView mj_loadNibWithOwner:self];
        _viewContent.alpha = 0;
        _viewContent.frame = CGRectMake(0, 0, SCREEN_WIDTH - 30*2, 170);
        
        _viewContent.center = self.center;
        _viewContent.layer.cornerRadius = 15;
        _viewContent.layer.masksToBounds = true;
        _viewContent.cancelBlock = ^{
            [weakSelf dismiss];
            if (weakSelf.cancelBlock)
                 weakSelf.cancelBlock();
        };
        _viewContent.confirmBlock = ^{
            [weakSelf dismiss];
            if (weakSelf.confirmBlock)
                 weakSelf.confirmBlock();
        };
        
    }
    return _viewContent;
    
}
#pragma mark **************** init
- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame])
    {
        
        [self addSubview:self.viewContent];
        self.contentview = self.viewContent;
        [self setUI];
        [self show];

    }
    return self;
}

- (void)setUI {
    
   
    
}


- (void)setConfigure:(MJUIAlertViewConfigure *)configure {
    _configure = configure;
    MJUIAlertContentView *viewContent = self.viewContent;

    if(configure.titleFont){
        viewContent.labTitle.font = configure.titleFont;
    }
    if(configure.titleColor){
        viewContent.labTitle.textColor = configure.titleColor;
    }
    if(configure.messageFont){
        viewContent.labContent.font = configure.messageFont;
    }
    if(configure.messageColor){
        viewContent.labContent.textColor = configure.messageColor;
    }
    if(configure.confirmTitle){
        [viewContent.btnConfirm setTitle:configure.confirmTitle forState:UIControlStateNormal];
    }
    if(configure.confirmButtonColor){
        [viewContent.btnConfirm setTitleColor:configure.confirmButtonColor forState:0];
    }
    if(configure.confirmButtonFont){
        viewContent.btnConfirm.titleLabel.font = configure.confirmButtonFont;
    }
    if(configure.cancelTitle){
        [viewContent.btnCancel setTitle:configure.cancelTitle forState:UIControlStateNormal];
    }
    if(configure.cancelButtonColor){
        [viewContent.btnCancel setTitleColor:configure.cancelButtonColor forState:0];
    }
    if(configure.cancelButtonFont){
        viewContent.btnCancel.titleLabel.font = configure.cancelButtonFont;
    }
    if(configure.hiddenCancelButton){
        viewContent.btnCancel.hidden = true;
        viewContent.viewHorLine.hidden = true;
    }
    
    
    
    viewContent.labContent.mj_lineSpacing = 5;
    CGSize messageSize = [viewContent.labContent sizeThatFits:CGSizeMake(SCREEN_WIDTH - 55*2, MAXFLOAT)];
    NSLog(@"MJLog=========messageSize==============%lf",messageSize.height);

    //内容计算行数
    CGFloat messageHeight = messageSize.height;
    CGFloat messageLineHeight = viewContent.labContent.font.lineHeight;
    
    NSInteger messageline = ceil(messageHeight)/messageLineHeight;
    if(messageline > 1){
        viewContent.labContent.textAlignment = NSTextAlignmentLeft;
    }
    
    if(configure.messageAlignmentCenter){
        viewContent.labContent.textAlignment = NSTextAlignmentCenter;
    }
    viewContent.labContent.lineBreakMode = NSLineBreakByWordWrapping; // 设置按单词换行

    //判断标题行数
    CGSize titleSize = [viewContent.labTitle sizeThatFits:CGSizeMake(SCREEN_WIDTH - 40*2, MAXFLOAT)];
    NSLog(@"MJLog=========titleSize==============%lf",titleSize.height);
    
    viewContent.mj_height = messageSize.height + titleSize.height + 130;
    
    if(configure.itemType == 1){
        viewContent.mj_height = messageSize.height + titleSize.height + 180;

        [viewContent.bottomView addSubview:viewContent.cornerButtonBg];
        viewContent.bottomViewH.constant = 90;
        [viewContent.cornerButtonBg mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(viewContent.bottomView);
        }];
        [viewContent.cornerLeftButton setTitle:configure.cancelTitle forState:UIControlStateNormal];
        [viewContent.cornerRightButton setTitle:configure.confirmTitle forState:UIControlStateNormal];
        
        [self setCornerButtonUI:viewContent.cornerLeftButton themeColor:configure.leftButtonTheme];
        [self setCornerButtonUI:viewContent.cornerRightButton themeColor:configure.rightButtonTheme];
       
    }
    
    if(configure.hiddenTitle){
        viewContent.labTitle.hidden = true;
//        viewContent.labContent.backgroundColor = UIColor.mj_redColor;
        viewContent.mj_height = viewContent.mj_height - titleSize.height;
        viewContent.labContentTop.constant = -20;
    }
    
    if(configure.isBottom){
        viewContent.center = CGPointMake(self.center.x, self.mj_height - viewContent.mj_height/2 - 30);
    }
}
- (void)setCornerButtonUI:(UIButton *)button themeColor:(UIColor *)themeColor{
    
    if (CGColorEqualToColor(themeColor.CGColor, UIColor.mj_appColor.CGColor)){
        
        [button setTitleColor:UIColor.mj_whiteColor forState:0];
        button.backgroundColor = UIColor.mj_appColor;
    }
    if (CGColorEqualToColor(themeColor.CGColor, UIColor.mj_grayColor.CGColor)){
        
        [button setTitleColor:UIColor.mj_blackColor forState:0];
        button.backgroundColor = UIColor.mj_backgroundColor1;
    }
   
}

@end
